<template>
  <div>
    <div class="card" style="margin-bottom: 20px;">
      <h2 style="margin-bottom: 15px;">手动库存管理</h2>
      
      <!-- 位置推荐面板 -->
      <el-card style="margin-bottom: 15px;">
        <div slot="header" class="clearfix">
          <span>位置推荐</span>
        </div>
        <el-form :model="recommendForm" label-width="120px">
          <el-form-item label="RFID信息">
            <el-input v-model="recommendForm.rfidInfo" placeholder="请输入RFID信息"></el-input>
          </el-form-item>
          <el-form-item label="对象类目ID">
            <el-input v-model="recommendForm.categoryId" placeholder="请输入对象类目ID"></el-input>
          </el-form-item>
          <el-form-item>
            <el-button type="primary" @click="getRecommendLocation">获取推荐位置</el-button>
          </el-form-item>
        </el-form>
        <div v-if="recommendResult" class="recommend-result">
          <el-alert title="推荐位置信息" type="success" :closable="false">
            <div>货架ID: {{ recommendResult.shelfId }}</div>
            <div>层数: {{ recommendResult.shelfLayer }}</div>
            <div>格子ID: {{ recommendResult.shelfCell }}</div>
          </el-alert>
        </div>
      </el-card>

      <!-- 入库管理面板 -->
      <el-card style="margin-bottom: 15px;">
        <div slot="header" class="clearfix">
          <span>入库管理</span>
        </div>
        <el-form :model="inboundForm" :rules="inboundRules" ref="inboundForm" label-width="120px">
          <el-form-item label="RFID信息" prop="rfidInfo">
            <el-input v-model="inboundForm.rfidInfo" placeholder="请输入RFID信息"></el-input>
          </el-form-item>
          <el-form-item label="对象唯一标识" prop="objectUuid">
            <el-input v-model="inboundForm.objectUuid" placeholder="请输入对象唯一标识"></el-input>
          </el-form-item>
          <el-form-item label="对象类目ID" prop="categoryId">
            <el-input v-model="inboundForm.categoryId" placeholder="请输入对象类目ID"></el-input>
          </el-form-item>
          <el-form-item label="货架ID" prop="shelfId">
            <el-select v-model="inboundForm.shelfId" placeholder="请选择货架">
              <el-option v-for="shelf in shelfList" :key="shelf.id" :label="`货架${shelf.id}`" :value="shelf.id"></el-option>
            </el-select>
          </el-form-item>
          <el-form-item label="货架层数" prop="shelfLayer">
            <el-input v-model.number="inboundForm.shelfLayer" placeholder="请输入货架层数"></el-input>
          </el-form-item>
          <el-form-item label="货架格子ID" prop="shelfCell">
            <el-input v-model.number="inboundForm.shelfCell" placeholder="请输入货架格子ID"></el-input>
          </el-form-item>
          <el-form-item label="对象级别" prop="level">
            <el-select v-model="inboundForm.level" placeholder="请选择对象级别">
              <el-option label="高级" value="1"></el-option>
              <el-option label="中级" value="2"></el-option>
              <el-option label="初级" value="3"></el-option>
            </el-select>
          </el-form-item>
          <el-form-item label="扩展信息">
            <el-input v-model="inboundForm.extJson" placeholder="请输入扩展信息(JSON格式)" type="textarea"></el-input>
          </el-form-item>
          <el-form-item>
            <el-button type="primary" @click="handleInbound">确认入库</el-button>
            <el-button @click="resetInboundForm">重置</el-button>
          </el-form-item>
        </el-form>
      </el-card>

      <!-- 出库管理面板 -->
      <el-card>
        <div slot="header" class="clearfix">
          <span>出库管理</span>
        </div>
        <el-form :model="outboundForm" :rules="outboundRules" ref="outboundForm" label-width="120px">
          <el-form-item label="RFID信息" prop="rfidInfo">
            <el-input v-model="outboundForm.rfidInfo" placeholder="请输入RFID信息"></el-input>
          </el-form-item>
          <el-form-item label="对象唯一标识" prop="objectUuid">
            <el-input v-model="outboundForm.objectUuid" placeholder="请输入对象唯一标识"></el-input>
          </el-form-item>
          <el-form-item label="对象类目ID" prop="categoryId">
            <el-input v-model="outboundForm.categoryId" placeholder="请输入对象类目ID"></el-input>
          </el-form-item>
          <el-form-item label="货架ID" prop="shelfId">
            <el-select v-model="outboundForm.shelfId" placeholder="请选择货架">
              <el-option v-for="shelf in shelfList" :key="shelf.id" :label="`货架${shelf.id}`" :value="shelf.id"></el-option>
            </el-select>
          </el-form-item>
          <el-form-item label="货架层数" prop="shelfLayer">
            <el-input v-model.number="outboundForm.shelfLayer" placeholder="请输入货架层数"></el-input>
          </el-form-item>
          <el-form-item label="货架格子ID" prop="shelfCell">
            <el-input v-model.number="outboundForm.shelfCell" placeholder="请输入货架格子ID"></el-input>
          </el-form-item>
          <el-form-item label="扩展信息">
            <el-input v-model="outboundForm.extJson" placeholder="请输入扩展信息(JSON格式)" type="textarea"></el-input>
          </el-form-item>
              <el-form-item>
            <el-button type="primary" @click="handleOutbound">确认出库</el-button>
            <el-button @click="resetOutboundForm">重置</el-button>
          </el-form-item>
        </el-form>
      </el-card>
    </div>
  </div>
</template>

<script>
import stockApi from '@/api/stock'

export default {
  data() {
    return {
      // 位置推荐相关
      recommendForm: {
        rfidInfo: '',
        categoryId: ''
      },
      recommendResult: null,
      
      // 入库相关
      inboundForm: {
        categoryId: '',
        extJson: '',
        level: '',
        objectUuid: '',
        operationType: 1, // 入库操作类型
        operator: JSON.parse(localStorage.getItem('system-user') || '{}').id || 0,
        rfidInfo: '',
        shelfCell: 0,
        shelfId: '',
        shelfLayer: 0,
        targetUserId: 0
      },
      inboundRules: {
        rfidInfo: [{ required: true, message: '请输入RFID信息', trigger: 'blur' }],
        objectUuid: [{ required: true, message: '请输入对象唯一标识', trigger: 'blur' }],
        categoryId: [{ required: true, message: '请输入对象类目ID', trigger: 'blur' }],
        shelfId: [{ required: true, message: '请选择货架', trigger: 'change' }],
        shelfLayer: [{ required: true, message: '请输入货架层数', trigger: 'blur' }],
        shelfCell: [{ required: true, message: '请输入货架格子ID', trigger: 'blur' }]
      },
      
      // 出库相关
      outboundForm: {
        categoryId: '',
        extJson: '',
        level: '',
        objectUuid: '',
        operationType: 2, // 出库操作类型
        operator: JSON.parse(localStorage.getItem('system-user') || '{}').id || 0,
        rfidInfo: '',
        shelfCell: 0,
        shelfId: 0,
        shelfLayer: 0,
        targetUserId: 0
      },
      outboundRules: {
        rfidInfo: [{ required: true, message: '请输入RFID信息', trigger: 'blur' }],
        objectUuid: [{ required: true, message: '请输入对象唯一标识', trigger: 'blur' }],
        categoryId: [{ required: true, message: '请输入对象类目ID', trigger: 'blur' }],
        shelfId: [{ required: true, message: '请选择货架', trigger: 'change' }],
        shelfLayer: [{ required: true, message: '请输入货架层数', trigger: 'blur' }],
        shelfCell: [{ required: true, message: '请输入货架格子ID', trigger: 'blur' }]
      },
      
      // 货架列表
      shelfList: []
    }
  },
  
  created() {
    this.loadShelfList()
  },
  
  methods: {
    // 加载货架列表
    async loadShelfList() {
      try {
        const res = await stockApi.getShelfList()
        if (res.success) {
          this.shelfList = res.data || []
        }
      } catch (error) {
        this.$message.error('获取货架列表失败')
      }
    },
    
    // 获取推荐位置
    async getRecommendLocation() {
      try {
        const res = await stockApi.recommendLocation(this.recommendForm)
        if (res.success) {
          this.recommendResult = res.data
          // 将推荐位置信息填充到入库表单
          this.inboundForm.shelfId = res.data.shelfId
          this.inboundForm.shelfLayer = res.data.shelfLayer
          this.inboundForm.shelfCell = res.data.shelfCell
        } else {
          this.$message.error(res.message || '获取推荐位置失败')
        }
      } catch (error) {
        this.$message.error('获取推荐位置失败')
      }
    },
    
    // 处理入库
    async handleInbound() {
      this.$refs.inboundForm.validate(async (valid) => {
        if (valid) {
          try {
            const res = await stockApi.inbound(this.inboundForm)
            if (res.success) {
              this.$message.success('入库成功')
              this.resetInboundForm()
            } else {
              this.$message.error(res.message || '入库失败')
            }
          } catch (error) {
            this.$message.error('入库失败')
          }
        }
      })
    },
    
    // 处理出库
    async handleOutbound() {
      this.$refs.outboundForm.validate(async (valid) => {
        if (valid) {
          try {
            const res = await stockApi.outbound(this.outboundForm)
            if (res.success) {
              this.$message.success('出库成功')
              this.resetOutboundForm()
            } else {
              this.$message.error(res.message || '出库失败')
            }
          } catch (error) {
            this.$message.error('出库失败')
          }
        }
      })
    },
    
    // 重置入库表单
    resetInboundForm() {
      this.$refs.inboundForm.resetFields()
    },
    
    // 重置出库表单
    resetOutboundForm() {
      this.$refs.outboundForm.resetFields()
    }
  }
}
</script>

<style scoped>
.card {
  background-color: #fff;
  padding: 20px;
  border-radius: 5px;
}

.recommend-result {
  margin-top: 15px;
}
</style>